package com.qs.eggs.view.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.BaseApplication;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.account.LoginActivity;
import com.qs.eggs.account.RegisterStepOneActivity;
import com.qs.eggs.adapter.BaseAdapter;
import com.qs.eggs.adapter.CategoryFirstMenuAdapter;
import com.qs.eggs.adapter.CategorySecondmenuAdapter;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.base.fragment.BaseFragment;
import com.qs.eggs.bean.Fenlei_item;
import com.qs.eggs.bean.Fenlei_items;
import com.qs.eggs.bean.GridViewItem;
import com.qs.eggs.view.activity.SortActivity;
import com.qs.eggs.view.prodetail.activity.ProDetailActivity;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 商品分类页，分左右两级展示商品，第一级是商品类型，第二级是详细的商品.
 */
public class CategoryFragment extends BaseFragment {
    private static GridView mGridView;
    private static RecyclerView mRecyclerView;
    private ArrayList<GridViewItem> list = new ArrayList<GridViewItem>();
    private ArrayList<Fenlei_items> arrarytwo = new ArrayList<>();
    private ArrayList<Fenlei_item> arrtotol = new ArrayList<Fenlei_item>();
    private CategoryFirstMenuAdapter adapterFirst;
    private CategorySecondmenuAdapter adaptertwo;
    String firstId;
    String page,page1,page2,page3,page4;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_category;
    }

    @Override
    protected void initData() {
        super.initData();
        HttpApi.FirstCategoryList(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                final ArrayList<Fenlei_item> arrarytotol = new ArrayList<Fenlei_item>();

                JSONArray jsonArr = null;
                try {
                    jsonArr = new JSONArray(s);
                    for (int i = 0; i < jsonArr.length(); i++) {
                        HashMap<String, String> idMap = new HashMap<String, String>();

                        String name = jsonArr.getJSONObject(i)
                                .getString("name");
                        firstId = jsonArr.getJSONObject(i)
                                .getString("id");
                        Fenlei_item item = new Fenlei_item(name, firstId);
                        arrarytotol.add(item);
                    }
                    setmGridViewFirst(arrarytotol);
                    //初始化二级列表
                    getSecondPro(arrtotol.get(0).getProid());


                } catch (JSONException e) {
                    // TODO Auto-generated catch bloc
                }
            }
        });
    }

    /**
     * FirstGridView
     */
    public void setmGridViewFirst(final ArrayList<Fenlei_item> arrarytotol) {

        arrtotol = arrarytotol;
        mGridView = findView(R.id.main_gridview);

        adapterFirst = new CategoryFirstMenuAdapter(getContext(), arrarytotol);
        mGridView.setAdapter(adapterFirst);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    final int position, long id) {
                // TODO Auto-generated method stub

                adapterFirst.setSeclect(position);
                adapterFirst.notifyDataSetChanged();

                getSecondPro(arrtotol.get(position).getProid());

            }
        });

    }
    /**
     * SecondMeun
     */
    public void setmRecyclerView(final ArrayList<Fenlei_items> arraryTwo) {

        mRecyclerView =findView(R.id.recyclerview);
        if (arraryTwo.size()>0){
           // arrarytwo = arraryTwo.get(0);
        }else {

        }
        adaptertwo = new CategorySecondmenuAdapter(getContext(), arraryTwo);
        adaptertwo.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getActivity(),ProDetailActivity.class);
                intent.putExtra("proid",arraryTwo.get(position).getProid());
                intent.putExtra("type",arraryTwo.get(position).getType());
                startActivity(intent);

            }
        });
        mRecyclerView.setAdapter(adaptertwo);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    /**
     * 二级商品列表的获取
     */

    private void getSecondPro(String proid){
        HttpApi.SecondCategoryList(proid,new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {

                final ArrayList<Fenlei_items> arrarytotol = new ArrayList<Fenlei_items>();

                JSONArray jsonArr = null;
                try {
                    jsonArr = new JSONArray(s);
                    ArrayList<HashMap<String, String>> idList = new ArrayList<>();
                    for (int i = 0; i < jsonArr.length(); i++) {
                        HashMap<String, String> idMap = new HashMap<String, String>();

                        String name = jsonArr.getJSONObject(i)
                                .getString("protypename");
                        String proid = jsonArr.getJSONObject(i)
                                .getString("id");
                        String type = jsonArr.getJSONObject(i)
                                .getString("type");
                        String img = Constants.URL_BASE+"productimages/"+ jsonArr.getJSONObject(i)
                                .getString("autoname");
                        idList.add(idMap);
                        Fenlei_items item = new Fenlei_items(img, name,proid,name,name,type);
                        arrarytotol.add(item);
                    }
                    setmRecyclerView(arrarytotol);

                } catch (JSONException e) {
                    // TODO Auto-generated catch bloc
                }
            }
        });
    }
    public void getProduct(int position){
        adapterFirst.setSeclect(position);

        getSecondPro(arrtotol.get(position).getProid());

    }
}
